<?php
/**
 * CRMLite Default Error Handling Controller
 *
 * @final
 * @package Crmlite_App
 * @version $Id:$
 * @copyright 2006-2007 ETE.CN
 * @author Yi BO  <etng2004@gmail.com>
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
require_once dirname(__FILE__) . '/BaseController.php';
class ErrorController extends BaseController
{
    /**
     * Exception Object
     *
     * $_error->type (Zend_Controller_Plugin_ErrorHandler constants)
     * $_error->exception (Exception object)
     *
     * @var object
     */
    protected $_error;

    /**
     * Controller Constructor
     *
     */
    public function init()
    {
        $this->_error = $this->getRequest()->getParam('error_handler');
    }

    /**
     * Error handler, central switch for errors passed by the ErrorHandler plugin
     *
     */
    public function errorAction()
    {
        switch ($this->_error->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                $this->_forward('e404');
                break;
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
            default:
                $this->_forward('e500');
                break;
        }

        // Disable viewrenderer
        $this->getHelper('ViewRenderer')->setNoRender();
    }

    /**
     * Page not found, throw HTTP 404 Error
     *
     */
    public function e404Action()
    {
        $this->view->error = $this->_error->exception->getMessage();
    }

    /**
     * Server Error, throw HTTP 500
     *
     */
    public function e500Action()
    {
        $this->view->error = $this->_error->exception->getMessage();
    }

    /**
     * This action is intended to be dispatched from a PageController,
     * this means that all of the conviences of the FrontController are not
     * here and we must do everything by hand as it relates to setting up
     * and rendering the view and passing the output to the response.
     *
     */
    public function setupErrorAction()
    {
        $this->view = new Zend_View();
        $this->view->setBasePath(dirname(dirname(__FILE__)) . '/views/');

        $this->getResponse()->setBody($this->view->render('error/setup-error.phtml'));
    }
}